home *** CD-ROM | disk | FTP | other *** search
/ The Atari Compendium / The Atari Compendium (Toad Computers) (1994).iso / files / prgtools / gnustuff / minix / update~4.z / update~4 / lib_stdio_fflush.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-09-06  |  899 b   |  45 lines

  1. /*                f f l u s h
  2.  *
  3.  * Flush a stream. The contents of the buffer are written to the
  4.  * file if the stream was opened for writing. The buffer pointers
  5.  * are reset to indicate an empty buffer.
  6.  *
  7.  * The function returns zero on success and EOF on failure.
  8.  *
  9.  * Patchlevel 1.0
  10.  *
  11.  * Edit History:
  12.  * 03-Sep-1989    Flush input buffer only if it exists.
  13.  */
  14.  
  15. #include "stdiolib.h"
  16.  
  17. /*LINTLIBRARY*/
  18.  
  19. int fflush(fp)
  20.  
  21. FILE *fp;                /* stream */
  22.  
  23. {
  24.   int length;                /* amount to flush */
  25.   int write();                /* write to channel */
  26.  
  27.   if (TESTFLAG(fp, _IOERR))
  28.     return EOF;
  29.  
  30.   if (! TESTFLAG(fp, _IOWRITE)) {
  31.     if (HASBUFFER(fp))
  32.       INITREADBUFFER(fp, 0);
  33.   }
  34.   else {
  35.     if (! TESTFLAG(fp, _IONBF) &&
  36.         (length = fp->_ptr - fp->_base) != 0 &&
  37.         write(fp->_file, (char *) fp->_base, length) != length)
  38.       return EOF;
  39.     if (HASBUFFER(fp))
  40.       INITWRITEBUFFER(fp);
  41.   }
  42.  
  43.   return 0;
  44. }
  45.